package gg.com.toolbox.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间类
 */
public class DateUtils {

    @SuppressWarnings("unused")
    private  SimpleDateFormat sf = null;

    /** 获取系统时间 格式为："yyyy" */
    public static String getDate_y() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        return sf.format(d);
    }

    /** 获取格式为："yyyy"的系统时间戳*/
    public static long getDateLong_y() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        try {
            date = sdf.parse(getDate_y());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 获取系统时间 格式为："MM" */
    public static String getDate_M() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("MM");
        return sf.format(d);
    }

    /** 获取格式为："MM"的系统时间戳*/
    public static long getDateLong_M() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        Date date = new Date();
        try {
            date = sdf.parse(getDate_M());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 获取系统时间 格式为："dd" */
    public static String getDate_d() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("dd");
        return sf.format(d);
    }

    /** 获取格式为："dd"的系统时间戳*/
    public static long getDateLong_d() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date date = new Date();
        try {
            date = sdf.parse(getDate_d());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 获取系统时间 格式为："yyyy-MM-dd" */
    public static String getDate_YMd() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    /** 获取格式为："yyyy-MM-dd"的系统时间戳*/
    public static long getDateLong_yMd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdf.parse(getDate_YMd());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 获取系统时间 格式为："yyyy-MM-dd HH:mm" */
    public static String getDate_YMdHm() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sf.format(d);
    }

    /** 获取格式为："yyyy-MM-dd HH:mm"的系统时间戳*/
    public static long getDateLong_yMdHm() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        try {
            date = sdf.parse(getDate_YMdHm());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 获取系统时间 格式为："yyyy-MM-dd HH:mm:ss" */
    public static String getDate_YMdHms() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /** 获取格式为："yyyy-MM-dd HH:mm:ss"的系统时间戳*/
    public static long getDateLong_yMdHms() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sdf.parse(getDate_YMdHms());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }


    /** 时间戳转换成字符窜 "yyyy"*/
    public static String longGetString_y(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        return sf.format(d);
    }

    /** 时间戳转换成字符窜 "MM"*/
    public static String longGetString_M(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("MM");
        return sf.format(d);
    }

    /** 时间戳转换成字符窜 "dd"*/
    public static String longGetString_d(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("dd");
        return sf.format(d);
    }

    /** 时间戳转换成字符窜 "yyyy-MM-dd"*/
    public static String longGetString_yMd(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    /** 时间戳转换成字符窜 "yyyy-MM-dd HH:mm"*/
    public static String longGetString_yMdHm(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sf.format(d);
    }

    /** 时间戳转换成字符窜 "yyyy-MM-dd HH:mm:ss"*/
    public static String longGetString_yMdHms(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /** 将字符串转为时间戳 "yyyy"*/
    public static long stringGetLong_y(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 将字符串转为时间戳 "MM"*/
    public static long stringGetLong_M(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 将字符串转为时间戳 "dd"*/
    public static long stringGetLong_d(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 将字符串转为时间戳 "yyyy-MM-dd"*/
    public static long stringGetLong_yMd(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 将字符串转为时间戳 "yyyy-MM-dd HH:mm"*/
    public static long stringGetLong_yMdHm(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /** 将字符串转为时间戳 "yyyy-MM-dd HH:mm:ss"*/
    public static long stringGetLong_yMdHms(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 将字符串转换为Date格式
     *
     * @param strDate yyyy-MM-dd HH:mm:ss格式字符串
     * @return
     */
    public static Date strParseDate(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 计算两个时间的时间间隔
     */
    public static String calculateDuration(Date start, Date end) {
        long duration = end.getTime() - start.getTime();
        int hour = (int) (duration / 3600 / 1000);
        int minute = (int) (duration % (3600 * 1000) / (60 * 1000));
        if (minute != 0 && hour != 0) {
            return hour + "小时" + minute + "分钟";
        }
        if (minute != 0 && hour == 0) {
            return minute + "分钟";
        }
        return "0分钟";
    }

    /**
     * 计算两个时间的时间间隔（分钟数）
     */
    public static int calculateDurationMinutes(Date start, Date end) {
        long duration = end.getTime() - start.getTime();
        int minute = (int) (duration / (60 * 1000));
        return minute;
    }

    /**
     * 获取分秒
     *
     * @param time 时间总长度
     * @return
     */
    public static String getTime(int time) {
        String str = "";
        int mtime = time;
        str = mtime / 60 + "分" + mtime % 60 + "秒";
        return str;
    }

    /**
     * 获取一个月有多少天
     *
     * @param YEAR  年
     * @param MONTH 月
     * @return
     */
    public static int getMonthDay(int YEAR, int MONTH) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, YEAR);
        cal.set(Calendar.MONTH, MONTH);//Java月份才0开始算
        int dateOfMonth = cal.getActualMaximum(Calendar.DATE);
        return dateOfMonth;
    }

    /**
     * 获取指定日期的前一天的日期
     *
     * @param time 指定的时间
     */
    public static String getYesterday(String time) {
        String s = "";
        String str[] = time.split("-");
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.set(Integer.parseInt(str[0]), Integer.parseInt(str[1]) - 1, Integer.parseInt(str[2]));//月份是从0开始的，所以11表示12月
        ca.setTime(new Date(ca.getTime().getTime() - AppConstant.oneDay));
        Date lastMonth = ca.getTime(); //结果
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        s = sf.format(lastMonth);
        return s;
    }


}